﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Shohdi.DataHandler;
using DatabaseComponent;

public partial class QuestionsCourseEN : System.Web.UI.Page
{



    public List<Courses> lstGridCourses
    {
        get
        {
            if (ViewState["lstGridCourses"] == null)
            {
                ViewState["lstGridCourses"] = System.Guid.NewGuid().ToString();
            }


            if (Session[ViewState["lstGridCourses"].ToString()] == null)
            {
                Session[ViewState["lstGridCourses"].ToString()] = new List<Courses>();

            }

            return (List<Courses>)Session[ViewState["lstGridCourses"].ToString()];

        }
        set
        {
            if (ViewState["lstGridCourses"] == null)
            {
                ViewState["lstGridCourses"] = System.Guid.NewGuid().ToString();
            }

            Session[ViewState["lstGridCourses"].ToString()] = value;
        }



    }


    



    /// <summary>
    /// it will check the security and customize the page
    /// </summary>
    /// <param name="currentPerson">the person to set security to his previleges</param>
    protected void checkSecurityAndCustomizePage(Persons currentPerson)
    {
        //Security Check

        if (!currentPerson.lstAllRulesIDs.Contains((long)RulesEnum.Questions_of_courses_page))
            Response.Redirect("LoginEN.aspx");



       





    }





   


    


   



    


   


   





    protected void Page_Load(object sender, EventArgs e)
    {


        Persons currentPerson = GlobalVars.getCurrentPerson();




        if (!Page.IsPostBack)
        {
            
            checkSecurityAndCustomizePage(currentPerson);


            BindGrid(new Folders());

            
            

        }
    }






























    /// <summary>
    /// this function will bind the courses grid by folder
    /// </summary>
    /// <param name="objFolder">the folder to bind the grid with</param>
    protected void BindGrid(Folders objFolder)
    {
        CoursesDB objCoursesDB = new CoursesDB(ConnectionStrings.LMS);

        List<Courses> lstCourses = new List<Courses>();


        if (objFolder.ID == -1)
        { 
            lstCourses = objCoursesDB.Select("", " where [FolderID] is null ");
        }
        else
        {
            lstCourses = objCoursesDB.Select("", " where [FolderID] = '" + objFolder.ID.ToString() + "' ");
        }

        


        lstGridCourses = lstCourses;

        grdUsers.DataSource = lstGridCourses;
        grdUsers.DataBind();


    }

   


   


    /// <summary>
    /// this will handle the selection change in the selected folder control
    /// </summary>
    /// <param name="sender">the object sended this event</param>
    /// <param name="e">the params that contains the selected item</param>
    protected void fcSelectFolder_SelectedItemChanged(object sender, CustomEventArgs e)
    {
        Folders i_objFolder = (Folders)e.objArgs;


        if (i_objFolder != null)
        {

            BindGrid(i_objFolder);

        }
        else
        {
            BindGrid(new Folders());
        }
    }











    protected void grdUsers_RowEditing(object sender, GridViewEditEventArgs e)
    {
        long selectedID = long.Parse(grdUsers.DataKeys[e.NewEditIndex]["ID"].ToString());

        string strID = System.Guid.NewGuid().ToString();

        Session[strID] = selectedID;

        Response.Redirect("~/LmsWebSite/QuestionsViewEN.aspx?id=" + strID);

    }


    protected void grdUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdUsers.DataSource = lstGridCourses;
        grdUsers.PageIndex = e.NewPageIndex;

        grdUsers.DataBind();
    }

}
